package cn.jvtc.course.management.system.dao.impl;

import cn.jvtc.course.management.system.bean.TeacherClassNexusDO;
import cn.jvtc.course.management.system.exception.TeacherClassNexusDaoException;
import cn.jvtc.course.management.system.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;

/**
 * 用于操作数据库的教师班级关系表
 *
 * @author GaoFeng2017
 * @date 2017-12-12 20:55:58
 **/

public class TeacherClassNexusDaoImpl {
    public boolean addTeacherClassNexus(TeacherClassNexusDO teacherClassNexusDO) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        int i = 0;
        String sql = "insert into teacher_class_nexus(gmt_create,class_id,teacher_id) values(?,?,?)";

        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
            st.setLong(2,teacherClassNexusDO.getClassId());
            st.setLong(3,teacherClassNexusDO.getTeacherId());
            i = st.executeUpdate();
        } catch(Exception e) {
            throw new TeacherClassNexusDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }

        if(i <= 0) {
            return false;
        }
        return true;
    }

    public boolean isNexusExist(long tid,long cid) {
        Connection con = null;
        PreparedStatement st = null;
        ResultSet rs = null;

        String sql = "select * from teacher_class_nexus where  teacher_id = ? and class_id = ?";

        try{
            con = JdbcUtils.getConnection();
            st = con.prepareStatement(sql);
            st.setLong(1,tid);
            st.setLong(2,cid);
            rs = st.executeQuery();
            if(!rs.next()) {
                return false;
            }
        } catch(Exception e) {
            throw new TeacherClassNexusDaoException(e);
        } finally {
            JdbcUtils.releaseResource(con,st,rs);
        }
        return true;
    }
}
